<!-- 关闭Tab时顶部标题 -->
<div class="layui-body-header">
    <span class="layui-body-header-title">树形表格</span>
    <span class="layui-breadcrumb pull-right">
        <a href="#/">首页</a>
        <a><cite>树形表格</cite></a>
    </span>
</div>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <div>
                <button class="layui-btn" id="tabTreeAdvBtnExpand">全部展开</button>
                <button class="layui-btn" id="tabTreeAdvBtnFold">全部折叠</button>
            </div>
            <table id="tabTreeAuthTable" class="layui-table" lay-filter="tabTreeAuthTable"></table>
        </div>
    </div>

</div>

<!-- 操作列 -->
<script type="text/html" id="tabTreeAuthState">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'treetable'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var treetable = layui.treetable;

        form.render('select');

        // 渲染表格
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'authorityId',
            treePidName: 'parentId',
            elem: '#tabTreeAuthTable',
            url: 'json/auths.json',
            page: false,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers'},
                {field: 'authorityName', minWidth: 200, title: '权限名称'},
                {field: 'authority', title: '权限标识'},
                {field: 'menuUrl', title: '菜单url'},
                {field: 'orderNumber', minWidth: 80, align: 'center', title: '排序号'},
                {
                    field: 'isMenu', minWidth: 80, align: 'center', templet: function (d) {
                        if (d.isMenu == 1) {
                            return '<span class="layui-badge layui-bg-gray">按钮</span>';
                        }
                        if (d.parentId == -1) {
                            return '<span class="layui-badge layui-bg-blue">目录</span>';
                        } else {
                            return '<span class="layui-badge-rim">菜单</span>';
                        }
                    }, title: '类型'
                },
                {templet: '#tabTreeAuthState', minWidth: 120, align: 'center', title: '操作'}
            ]],
            done: function () {
                layer.closeAll('loading');
            }
        });

        //监听工具条
        table.on('tool(tabTreeAuthTable)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值

            if (layEvent === 'edit') { //查看
                layer.msg('点击了修改');
            } else if (layEvent === 'del') { //删除
                layer.msg('点击了删除');
            }
        });

        $('#tabTreeAdvBtnExpand').click(function () {
            treetable.expandAll('#tabTreeAuthTable');
        });

        $('#tabTreeAdvBtnFold').click(function () {
            treetable.foldAll('#tabTreeAuthTable');
        });

    });
</script>